package com.king.nio.zerocopy;

import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.Socket;

/**
 * 传统 IO 客户端
 * @author king
 * @date 2021/1/1 23:42
 */
public class OldIOClient {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket("localhost", 7001);

        String fileName = "test.pdf";
        FileInputStream fileInputStream = new FileInputStream(fileName);

        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());

        byte[] bytes = new byte[4096];
        long readCount;
        long total = 0;

        long startTime = System.currentTimeMillis();

        while((readCount = fileInputStream.read(bytes)) >= 0){
            total += readCount;
            dataOutputStream.write(bytes);
        }

        System.out.println("发生总字节数：" + total + ",耗时：" + (System.currentTimeMillis() - startTime));
    }
}
